<template>
	<div class="sys-pos-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" class="fl">
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
					<el-form-item label="编号" prop="wcsTemLogicNo"
						:rules="[{ required: true, message: '编号不能为空', trigger: 'blur' }]" align="center">
						<el-input v-model="state.ruleForm.wcsTemLogicNo" placeholder="模板编号" clearable class="input" />
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
					<el-form-item label="名称" prop="wcsTemlogicName"
						:rules="[{ required: true, message: '编号不能为空', trigger: 'blur' }]" align="center">
						<el-input v-model="state.ruleForm.wcsTemlogicName" placeholder="模板名称" clearable class="input" />
					</el-form-item>
				</el-col>
				<div style="border: 2px solid var(--el-color-primary);padding: 20px">


					<el-row>
						<el-col :span="8" class="center-align">
							<span style="font-size: 12px;">子任务名称</span>
						</el-col>
						<el-col :span="8" class="center-align">
							<span style="font-size: 12px;">重复执行</span>
						</el-col>
						<el-col :span="8" class="center-align">
							<el-icon :size="20" @click="addInput">
								<Plus />
							</el-icon>
						</el-col>
					</el-row>
					<el-row>
						<div v-for="(item, index) in state.ruleForm.subTempDetail" style="display: flex;width:100%;"
							:key="item.ModelNo">

							<div style="flex:1;margin-bottom: 5px" class="center-align" >
								<el-input type="text" v-model="item.ModelNo" placeholder="输入任务内容"
									@input="updateInput(index, $event.target.value)" @click="openEditComponent(index)"
									clearable @clear="removeInput(index)" />
							</div>

							<div style="flex:1;margin-bottom: 5px" class="center-align">
								<el-switch v-model="item.deduplicate"
									style="--el-switch-on-color: var(--el-color-primary); --el-switch-off-color: #bbb" />
							</div>

							<div style="flex:1"></div>
						</div>
					</el-row>
				</div>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
		<editRCSModel ref="subEditComponent" :title="state.editTitle" @handleQuery="addModelNoValue" />
	</div>
</template>

<script lang="ts" setup name="sysEditPos">
import { reactive, ref } from 'vue';
import { getAPI } from '/@/utils/axios-utils';
import { SysPosApi } from '/@/api-services/api';
import { WmsApi } from '/@/api/wh';
import { ElMessage } from 'element-plus';
import editRCSModel from "/@/views/system/wcsModel/editRCSModel.vue";
const subEditComponent = ref()
const props = defineProps({
	title: String,
});
const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref();
const state = reactive({
	editTitle: '',
	isShowDialog: false,
	ruleForm: {
		"wcsTemlogicName": undefined,
		"wcsTemLogicNo": undefined,
		"subTempDetail": [
			{
				ModelNo: '',
				temlogicName: '',
				deduplicate: false,
				subTaskSeq: 1
			} // 初始的 input 框的值  
		]
	},
	isEditing: false
});

// 打开弹窗
const openDialog = (row: any, isEditing: boolean) => {
	state.ruleForm = JSON.parse(JSON.stringify(row));
	state.isEditing = isEditing
	state.isShowDialog = true;
	console.log(state.ruleForm);
	ruleFormRef.value?.resetFields();
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

const addInput = () => {
	console.log(state.ruleForm.subTempDetail);
	console.log("被点击了");

	state.ruleForm.subTempDetail.push({ ModelNo: '' });
}
const addEdit = () => {
	state.ruleForm.subTempDetail.push({ ModelNo: '' });
	console.log(state.ruleForm.subTempDetail.length);

}
//选择RCS模板
const openEditComponent = (index: number) => {
	state.editTitle = "选择RCS模板";
	subEditComponent.value.openDialog();
}
const addModelNoValue = (info: any) => {
	//把选择的子模板号传给对象，把名称传给页面用于显示

	console.log(info);
	// if (state.isEditing) {
	state.ruleForm.subTempDetail[state.ruleForm.subTempDetail.length - 1].ModelNo = info[0].TemplateNo
	state.ruleForm.subTempDetail[state.ruleForm.subTempDetail.length - 1].temlogicName = info[0].temlogicName

	// } else {


	// state.ruleForm2.subTempDetail[state.ruleForm2.subTempDetail.length - 1].ModelNo = info[0].modelProcessCode
	// state.ruleForm2.subTempDetail[state.ruleForm2.subTempDetail.length - 1].ModelName = info[0].temlogicName
	// }

}
// 提交
const submit = () => {
	console.log(state.ruleForm);

	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		//处理子序列的编号
		state.ruleForm.subTempDetail.forEach((item, index) => {
			item.subTaskSeq = index + 1
		})
		if (state.isEditing) {


			if (!valid) return false;

			await WmsApi().ModifyWcsModel(state.ruleForm)
				.then(function (res) {
					ElMessage.success('修改成功');

					closeDialog();
				})
				.catch(function (error) {
					console.log(error);

				});


		} else {


			if (!valid) return false;

			WmsApi().addWcsModel(state.ruleForm)
				.then(function (res) {
					ElMessage.success('新增成功');

					closeDialog();
				})
				.catch(function (error) {
					console.log(error);

				});


		}

	});
};
const removeInput = (index: number) => {
	if (index > -1) { // 确保不会删除未定义的索引  
		state.ruleForm.subTempDetail.splice(index, 1);
		console.log(state.ruleForm2.subTempDetail);

	}
}
const removeEdit = (index: number) => {
	if (index > -1) { // 确保不会删除未定义的索引  
		state.ruleForm.subTempDetail.splice(index, 1);
		console.log(state.ruleForm2.subTempDetail);

	}
}
const updateInput = (index: number, value: any) => {
	// 直接修改数组中的对象属性，Vue 3 会自动追踪变化  
	state.ruleForm.subTempDetail[index].ModelNo = value;
	// 这里也可以添加逻辑来处理或保存输入的值  
	console.log(`输入的值：${value}`);
};
// 导出对象
defineExpose({ openDialog });
</script>
<style lang="scss">
.fl {
	width: 70%;
	margin: 20px auto;
}

.center-align {
	display: flex;
	justify-content: center;
	/* 水平居中 */
	align-items: center;
	/* 垂直居中 */
	/* 如果你还想让 span 内的文本垂直居中（对于单行文本），可以添加 */
	text-align: center;
}
</style>